using System;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using UserInterface.Misc;
using Domain.Entities;
using Domain.Services;

namespace UserInterface.PM
{
    public partial class Read : BoardPageBase
    {
        private PrivateMessage message;
        private string referer;
        
        protected void Page_Load(object sender, EventArgs e)
        {
            int messageId;
            if (!int.TryParse(Request.QueryString["messageId"], out messageId))
            {
                DieMessage(DieMessageType.Error, "PrivateMessageNotFound", "/PM/Inbox.aspx");
            }

            message = PrivateMessageService.GetById(messageId);
            referer = Request.QueryString["ref"];

            User user = CurrentSession.User;

            if (!PermissionManager.IsAdministrator()
                && message.Sender != user)
            {
                DieMessage(DieMessageType.InsufficientPermissions, "InsufficientPermissions");
            }

            if (!IsPostBack)
            {
                if (message.Receiver == user)
                {
                    PrivateMessageService.MarkRead(message);
                }
                ShowMessage();
            }
        }

        protected void ShowMessage()
        {
            string pageTitle = String.Format(
                (string)GetLocalResourceObject("Title"),
                message.Subject
            );

            MasterPage.TitleGenerator.PageTitle = pageTitle;
            MasterPage.HeaderText = pageTitle;

            if (referer == "outbox")
            {
                Menu.IsOutboxActive = true;
                MasterPage.AddPathItem((string)GetLocalResourceObject("Outbox"), "/PM/Outbox.aspx");
            }
            else if (referer == "archive")
            {
                Menu.IsArchiveActive = true;
                MasterPage.AddPathItem((string)GetLocalResourceObject("Archive"), "/PM/Archive.aspx");
            }
            else if (referer == "recyclebin")
            {
                Menu.IsRecycleBinActive = true;
                MasterPage.AddPathItem((string)GetLocalResourceObject("RecycleBin"), "/PM/RecycleBin.aspx");
            }
            else if (referer == "search")
            {
                Menu.IsSearchActive = true;
                MasterPage.AddPathItem((string)GetLocalResourceObject("Search"), "/PM/Search.aspx");
            }
            else
            {
                Menu.IsInboxActive = true;
                MasterPage.AddPathItem((string)GetLocalResourceObject("Inbox"), "/PM/Inbox.aspx");
            }

            MasterPage.AddPathItem(pageTitle, "/PM/Read.aspx?messageId=" + message.Id + (referer == null ? "" : "&amp;ref=" + referer));

            bool IsUserReceiver = message.Receiver == CurrentSession.User;

            MarkReadButton.Visible = IsUserReceiver && !message.IsRead;
            MarkUnreadButton.Visible = IsUserReceiver && message.IsRead;
            ArchiveButton.Visible = IsUserReceiver && !message.IsArchived;

            FromLiteral.Text = message.Sender.Username;
            ToLiteral.Text = message.Receiver.Username;
            SubjectLiteral.Text = message.Subject;
            DateSentLiteral.Text = OutputTools.FormatDate(message.DateSent);
            ContentLiteral.Text = message.Content;
        }

        protected void MarkReadButton_Click(object sender, EventArgs e)
        {
            PrivateMessageService.MarkRead(message);
            ShowMessage();
        }

        protected void MarkUnreadButton_Click(object sender, EventArgs e)
        {
            PrivateMessageService.MarkUnread(message);
            ShowMessage();
        }

        protected void ArchiveButton_Click(object sender, EventArgs e)
        {
            PrivateMessageService.Archive(message);
            referer = "archive";
            ShowMessage();
        }

        protected void DeleteButton_Click(object sender, EventArgs e)
        {
            PrivateMessageService.Trash(CurrentSession.User, message);
            ShowMessage();
        }

        protected int SenderId
        {
            get { return message.Sender.Id; }
        }

        protected int ReceiverId
        {
            get { return message.Receiver.Id; }
        }
    }
}
